package com.nick.sscplan.provider;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.nick.sscplan.bean.SscInfo;
import com.nick.sscplan.util.SscUtils;

public class SscDAO extends SscDataBase{

    
    public SscDAO(Context context) {
        super(context);
    }
    
    public boolean insertSscInfo(SscInfo sscInfo){
        ContentValues values = new ContentValues();
        //values.put(SscUtils.SSC_ID, sscInfo.get_id());
        values.put(SscUtils.SSC_ISSUE, sscInfo.getIssue());
        values.put(SscUtils.SSC_DATA1, sscInfo.getData1());
        values.put(SscUtils.SSC_DATA2, sscInfo.getData2());
        values.put(SscUtils.SSC_DATA3, sscInfo.getData3());
        values.put(SscUtils.SSC_DATA4, sscInfo.getData4());
        values.put(SscUtils.SSC_DATA5, sscInfo.getData5());
        values.put(SscUtils.SSC_OPENTIME, sscInfo.getOpentime());
        values.put(SscUtils.SSC_REMARK, sscInfo.getRemark());
        
        return mDatabase.insert(TABLE_NAME, null, values)>0 ? true : false;
    }
    
    public List<SscInfo> queryAllSscInfo(){
        List<SscInfo> sscInfos = new ArrayList<SscInfo>();
        Cursor cursor = mDatabase.query(TABLE_NAME, null, null, null, null, null, null);
        while(cursor.moveToNext()){
            SscInfo sscInfo = new SscInfo();
            sscInfo.set_id(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_ID)));
            sscInfo.setIssue(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_ISSUE)));
            sscInfo.setData1(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_DATA1)));
            sscInfo.setData2(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_DATA2)));
            sscInfo.setData3(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_DATA3)));
            sscInfo.setData4(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_DATA4)));
            sscInfo.setData5(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_DATA5)));
            sscInfo.setOpentime(cursor.getString(cursor.getColumnIndex(SscUtils.SSC_OPENTIME)));
            sscInfo.setRemark(cursor.getString(cursor.getColumnIndex(SscUtils.SSC_REMARK)));
            sscInfos.add(sscInfo);
        }
       cursor.close();
       return sscInfos;
    }
    
    public SscInfo queryByIssue(long issue){
        SscInfo sscInfo = new SscInfo();
        Cursor cursor = mDatabase.query(TABLE_NAME, null, "issue ="+issue, null, null, null, null);
        sscInfo.set_id(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_ID)));
        sscInfo.setIssue(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_ISSUE)));
        sscInfo.setData1(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_DATA1)));
        sscInfo.setData2(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_DATA2)));
        sscInfo.setData3(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_DATA3)));
        sscInfo.setData4(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_DATA4)));
        sscInfo.setData5(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_DATA5)));
        sscInfo.setOpentime(cursor.getString(cursor.getColumnIndex(SscUtils.SSC_OPENTIME)));
        sscInfo.setRemark(cursor.getString(cursor.getColumnIndex(SscUtils.SSC_REMARK)));
        return sscInfo;
    }
    
    
    public List<SscInfo> queryByDate(String date){
        List<SscInfo> sscInfos = new ArrayList<SscInfo>();
        Cursor cursor = mDatabase.query(TABLE_NAME, null, SscUtils.SSC_OPENTIME + " LIKE " + "'" +date + "%" + "'", null, null, null, null);
        while(cursor.moveToNext()){
            SscInfo sscInfo = new SscInfo();
            sscInfo.set_id(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_ID)));
            sscInfo.setIssue(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_ISSUE)));
            sscInfo.setData1(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_DATA1)));
            sscInfo.setData2(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_DATA2)));
            sscInfo.setData3(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_DATA3)));
            sscInfo.setData4(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_DATA4)));
            sscInfo.setData5(cursor.getInt(cursor.getColumnIndex(SscUtils.SSC_DATA5)));
            sscInfo.setOpentime(cursor.getString(cursor.getColumnIndex(SscUtils.SSC_OPENTIME)));
            sscInfo.setRemark(cursor.getString(cursor.getColumnIndex(SscUtils.SSC_REMARK)));
            sscInfos.add(sscInfo);
        }
       cursor.close();
       return sscInfos;
    }
    
    
    public boolean updateData(List<SscInfo> sscInfos){
        
        for (int i = 0; i < sscInfos.size(); i++) {
            ContentValues values = new ContentValues();
            values.put(SscUtils.SSC_ISSUE, sscInfos.get(i).getIssue());
            values.put(SscUtils.SSC_DATA1, sscInfos.get(i).getData1());
            values.put(SscUtils.SSC_DATA2, sscInfos.get(i).getData2());
            values.put(SscUtils.SSC_DATA3, sscInfos.get(i).getData3());
            values.put(SscUtils.SSC_DATA4, sscInfos.get(i).getData4());
            values.put(SscUtils.SSC_DATA5, sscInfos.get(i).getData5());
            values.put(SscUtils.SSC_REMARK, sscInfos.get(i).getRemark());
            mDatabase.update(TABLE_NAME, values, null, null);
        }
        
        return false;
    }
    
    public boolean deleteAllData(){
        return mDatabase.delete(TABLE_NAME, null, null)>0 ? true : false;
        
    }
    
    
    

}
